<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

//TODO rewrite sql queries into use CI query model

class Accessmodel extends Model {

	var $user_id = 0;

	var $page_id = null;
	var $urls = null;

	var $group_id = 0;
	var $group = array();

	function __construct () {
		parent::Model();
		$this->load->database();
	}

	/**
	 * Check user permissions to get page.
	 * First we need to check if user is in the group which has access to that page.
	 * Second we need to check if user has custom permissions for access to that page
	 */
	function check () {
		
		return $this->checkGroup() || $this->checkCustom();
	}

	function get () {
		if ($this->user_id) {
			return array(
				'groups' => $this->getGroups(),
				'pages' => $this->getPages(),
			);
		} elseif ($this->group_id) {
			return $this->getGroup();
		} elseif ($this->page_id) {
			return $this->getPage();
		} else {
			return null;
		}
	}

	function checkGroup () {
		
		$this->db->select('ag.id');
		$this->db->from('accessgroups AS `ag`');
		$this->db->join('access_group_user AS `agu`', 'ag.id=agu.group_id', 'INNER');
		$this->db->join('access_group_pages AS `agp`', 'ag.id=agp.group_id', 'INNER');
		$this->db->join('`pages AS `p`', 'agp.page_id=p.id', 'INNER');
		$this->db->where('agu.user_id', $this->user_id);
		
		$path = '';
		$paths = array();
		foreach ($this->urls as $segment) {
			if ($path) { 
				$paths[] = $path . "/*"; 
			}
			$path .= '/' . $segment;
		}
		
		$paths[] = $path;
		
		if (count($paths) > 1) { 
			$this->db->where_in('p.page', $paths);
		}
		else { 
			$this->db->where('p.page', $paths[0]);
		}

		return (bool)$this->db->get(null, 1)->num_rows;
	}

	function checkCustom () {
		
		$sql = 'SELECT `p`.`id` FROM `accesscustom` `ac` ' .
			"INNER JOIN `pages` `p` ON `ac`.`object_id`=`p`.`id` AND `ac`.`object_type` = 'page' " . 
			'WHERE `ac`.`user_id` = ' . $this->user_id; 
		
		$path = '';
		$paths = array();
		foreach ($this->urls as $segment) {
			if ($path) { $paths[] = "`p`.`page` = '" . $path . "/*'"; }
			$path .= '/' . $segment;
		}
		
		$paths[] = "`p`.`page` = '" . $path . "'";
		
		if (count($paths) > 1) { $sql .= ' AND (' . join(' OR ', $paths) . ')'; }
		else { $sql .= ' AND ' . $paths[0]; }
		
		$sql .= ' LIMIT 1';
		
		$result = $this->db->query($sql);
		return (bool)$result->num_rows;
	}

//	function getUserPermissions () {
//
//		$this->db->select('`accessgroups`.`id`, `access_group_user`.`user_id`');
//		$this->db->from('accessgroups');
//		$this->db->join('`accessgroups`', '`accessgroups`.`id`=`access_group_user`.`user_id`', 'LEFT');//TODO MAYBE RIGHT
//		$this->db->where('`access_group_user`.`user_id`', $this->user_id);
//		return $this->db->get('accessgroups')->result();
//	}

	//TODO check LEFT RIGHT joins
	function getGroups () {

		$select = 'accessgroups.id, accessgroups.name';
		$this->db->from('accessgroups');
//		if ($this->user_id) {
//			$select .= ', `access_group_user`.`user_id`';
//			$this->db->join('`access_group_user`', '`accessgroups`.`id`=`access_group_user`.`user_id`', 'RIGHT');//TODO MAYBE RIGHT
//			$this->db->where('`access_group_user`.user_id', $this->user_id);
//		}
		$this->db->select($select);
		return $this->db->get('accessgroups')->result();
	}

	function getUsers () {
		$this->db->select('users.id, users.login');
		$this->db->from('users');
		return $this->db->get('users')->result();
	}

	//TODO check LEFT RIGHT joins
	function getPages () {

		$select = 'pages.id, pages.page';
		$this->db->from('pages');
//		if ($this->user_id) {
//
//			$select .= ', accessgroups.id AS group, accesscustom.user_id AS custom';
//			$this->db->join('access_group_pages', 'access_group_pages.page_id=pages.id', 'LEFT');
//			$this->db->join('accessgroups', 'access_group_pages.group_id=accessgroups.id', 'INNER');
//			$this->db->join('access_group_user', 'access_group_user.group_id=accessgroups.id', 'INNER');
//			$this->db->where('access_group_user.user_id', $this->user_id);
//
//			$this->db->join('accesscustom', 'accesscustom.page_id=pages.id', 'LEFT');
//			$this->db->where('accesscustom.user_id', $this->user_id);
//		}

		$this->db->select($select);
		return $this->db->get('pages')->result();
	}

	function getGroup () {
		$this->db->from('accessgroups');
		$this->db->where('id', $this->group_id);
		$this->group = $this->db->get('accessgroups', 1)->result();

		$this->db->select('pages.id, pages.page');
		$this->db->from('pages');
		$this->db->join('access_group_pages', 'access_group_pages.page_id=pages.id', 'INNER');
		$this->db->where('access_group_pages.group_id', $this->group_id);
		$this->group['pages'] = $this->db->get('pages')->result();

		$this->db->select('user.id, user.login');
		$this->db->from('users');
		$this->db->join('access_group_user', 'access_group_user.user_id=users.id', 'INNER');
		$this->db->where('access_group_user.group_id', $this->group_id);
		$this->group['users'] = $this->db->get('users')->result();
		return $this->group;
	}

	function saveGroup () {
		$data = array('name' => $this->group['name']);

		if ($this->group_id) {
			$this->db->where('id', $this->group_id);
			$this->db->update('mytable', $data);
		} else {
			$res = $this->db->insert('accessgroups', $data);
			$this->group_id = $this->db->insert_id();
		}

		foreach ($this->group['pages'] as $page) {
			$data = array('group_id' => $this->group_id, 'page_id' => $page);
			$res = $res && $this->db->insert('access_group_pages', $data);
		}
		foreach ($this->group['users'] as $user) {
			$data = array('group_id' => $this->group_id, 'user_id' => $user);
			$res = $res && $this->db->insert('access_group_user', $data);
		}
		return $res;
	}

	function getPage () {
		$this->db->from('accesspage');
		$this->db->where('id', $this->page_id);
		$this->page = $this->db->get('accesspage', 1)->result();

		$this->db->select('accessgroup.id, accessgroup.name');
		$this->db->from('accessgroups');
		$this->db->join('access_group_pages', 'access_group_pages.page_id=pages.id', 'INNER');
		$this->db->where('access_group_pages.page_id', $this->page_id);
		$this->page['groups'] = $this->db->get('accessgroups')->result();

		$this->db->select('user.id, user.login');
		$this->db->from('users');
		$this->db->join('accesscustom', 'accesscustom.user_id=users.id', 'INNER');
		$this->db->where('accesscustom.page_id', $this->pages_id);
		$this->page['users'] = $this->db->get('users')->result();
		return $this->group;
	}

	function savePage () {
		$data = array('id' => $this->page_id, 'name' => $this->page['page']);
		$res = $this->db->insert('pages', $data);

		foreach ($this->page['groups'] as $group) {
			$data = array('group_id' => $group, 'page_id' => $this->page_id);
			$res = $res && $this->db->insert('access_group_pages', $data);
		}
		foreach ($this->group['users'] as $user) {
			$data = array('group_id' => $this->group_id, 'user_id' => $user);
			$res = $res && $this->db->insert('accesscustom', $data);
		}
		return $res;
	}
}
